<?php
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_ClassDirectoryLocator
 *
 *	This class recursively iterates through a directory looking for a class filename match.
 *	Developer's note; All class files have their class name as lowercase file names - (class) Foo becomes foo.php etc.
 *
 *	@package Core
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_ClassDirectoryLocator {

	/**
	 *	@var string $base_path
	 */
	protected $base_path;
	
	/**
	 *	@var string $class_path
	 */
	protected $class_path;
	
	/**
	 *	__construct
	 *
	 *	@param string $class_name
	 *	@param string $base_path
	 */
	public function __construct($class_name, $base_path) {
		$this->base_path = $base_path;
		$this->locate(strtolower($class_name) . ARCH_FILE_EXT);
	}
	
	/**
	 *	locate
	 *
	 *	This method recursively iterates through a directory looking for a class filename match. It uses RecursiveIteratorIterator and RecursiveDirectoryIterator found in the SPL. For more information see - http://www.php.net/~helly/php/ext/spl/
	 *
	 *	@param string $file_name
	 */
	private function locate($file_name) {
		$directories = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->base_path), true);
	
		foreach($directories as $directory) {
			if($directory->getFilename() === $file_name) {
				$this->class_path = $directory->getPath();
				return true;
			}
		}
		
	}
	
	/**
	 *	__toString
	 */
	public function __toString() {
		if(is_string($this->class_path))
			return $this->class_path . DIRECTORY_SEPARATOR;
	return '';
	}

}
?>